package org.jeecg.modules.flow.controller.delete;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LiveQingTest {
    //接口地址
    static final String api = "http://192.168.70.158:28000";
    //设备ID
    static final String deviceId = "15WbDL1SR123";

    /**
     * 登录
     *
     * @return
     */
    private static String login() {
        //admin 和 密码，密码用 md5 加密
        String username = "admin", password = SecureUtil.md5("mima"), token = "";
        try {
            //Get登录
            String result = HttpUtil.get(api + "/api/v1/login?username=" + username + "&password=" + password);
            //打印解析
            log.info(result);
            //返回后解析Json （使用fastJson打印）
            JSONObject jsonObject = JSONObject.parseObject(result);
            //如果为200
            if (jsonObject.getInteger("code") == 200) {
                //jsonObject在获取JsonObject。获取data
                JSONObject dataJson = jsonObject.getJSONObject("data");
                //返回结果的token
                token = dataJson.getString("token");
            }
        } catch (Exception exception) {
            log.error(exception.getLocalizedMessage());
        }
        return token;
    }

    /**
     * 状态  newPushSession:推流中，closePushSession:停止推流
     *
     * @param deviceId
     * @return
     */
    private static String getDeviceState(String deviceId) {
        String type = null;
        //获取推流状态
        String result = HttpUtil.get(api + "/api/v1/live/state?id=" + deviceId);
        log.info(result);
        JSONObject jsonObject = JSONObject.parseObject(result);

        if (jsonObject.getInteger("code") == 200) {
            JSONObject dataJson = jsonObject.getJSONObject("data");
            type = dataJson.getString("type");
        }
        return type;
    }

    /**
     * 获取直播列表  all:所有直播(默认值) living:直播中，unstart：尚未直播
     *
     * @param token
     * @return
     */
    private static JSONObject getLiveList(String token, String state) {
        //获取开放直播列表
        String result = HttpUtil.get(api + "/api/v1/live/list?state=" + state + "&start=0&limit=100&token=" + token);
        log.info(result);
        JSONObject jsonObject = JSONObject.parseObject(result);

        if (jsonObject.getInteger("code") == 200) {
            JSONObject dataJson = jsonObject.getJSONObject("data");
            log.info(dataJson.toJSONString());
            return dataJson;
        }
        return null;
    }

    //m3u8
    private static String getM3u8Url(String deviceId, String starttime, String endtime) {
        //指定时间段录像播放(连续录像hls播放)
        String videoUrl = api + "/api/v1/record/video/hls/" + deviceId + "/" + starttime + "/" + endtime + "/video.m3u8";
        String result = HttpUtil.get(videoUrl);
        log.info(videoUrl);
        return videoUrl;
    }

    //mp4
    private static String getMp4Url(String deviceId, String starttime, String endtime) {
        //指定时间段录像播放(连续录像mp4播放)
        System.out.println(DateUtil.now());
        //组装URL，进行返回
        String videoUrl = api + "/api/v1/record/video/play/" + deviceId + "/" + starttime + "/" + endtime + "/video.mp4";

        //这里使用了xkcoding的simple-http，使用 hutool的更好，可以设置 超时时间
        String result = HttpUtil.get(videoUrl);

        log.info(videoUrl);
        //使用 hutool工具类 打印时间
        System.out.println(DateUtil.now());

        return videoUrl;
    }

    public static void main(String[] args) {
        try {
            //String token = login();
            //String state = getDeviceState(deviceId);
            //JSONObject living = getLiveList(token, "living");

            String starttime = "20240322154500", endtime = "20240322154600";
            //String m3u8Url = getM3u8Url(deviceId, starttime, endtime);
            String mpuUrl = getMp4Url(deviceId, starttime, endtime);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}
